package com.my;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by YJH on 2017/6/24 15:07.
 */
public class LoginServlet implements Servlet {

    private String username;
    private String password;

    public void init(ServletConfig servletConfig) throws ServletException {
        ServletContext context = servletConfig.getServletContext();
        this.username = context.getInitParameter("username");
        this.password = context.getInitParameter("password");
    }

    public ServletConfig getServletConfig() {
        return null;
    }

    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        //1.获取请求参数：username，password
        String username = servletRequest.getParameter("username");
        String password = servletRequest.getParameter("password");
        //2.获取当前WEB应用初始化的参数：username，password
        //3.对比
        PrintWriter out = servletResponse.getWriter();
        if (this.username.equals(username) && this.password.equals(password)) {
            out.println("success");
        }else {
            out.println("error");
        }
    }

    public String getServletInfo() {
        return null;
    }

    public void destroy() {

    }
}
